﻿SSVC.Wrap.Dojo.Declare("ActiveLineElement", SSVC.SVG.ActiveElement,
{
	startX: null,
	startY: null,
	endX: null,
	endY: null,

	IsVector: false,

	_addInnerElement: function()
	{
		this.InnerElement = new SSVC.SVG.Line();
	},

	_addSelectionTops: function()
	{
		var top1 = new SSVC.SVG.Circle();
		top1.Class = "Top";
		top1.Name = "top1";
		top1.Number = SSVC.SVG.ActiveElement.Enums.ResizingTops.TopLeft;

		var top2 = new SSVC.SVG.Circle();
		top2.Class = "Top";
		top2.Name = "top1";
		top2.Number = SSVC.SVG.ActiveElement.Enums.ResizingTops.BottomRight;

		SSVC.Wrap.Dojo.On(top1.Node, "mousedown", SSVC.Wrap.Dojo.Partial(this.OnTopMouseDown, this, top1.Number));
		SSVC.Wrap.Dojo.On(top2.Node, "mousedown", SSVC.Wrap.Dojo.Partial(this.OnTopMouseDown, this, top2.Number));

		this.Tops.push(top1);
		this.Tops.push(top2);

		this.Node.appendChild(top1.Node);
		this.Node.appendChild(top2.Node);

		this._setCursorAttrForTops();
	},

	_setCursorAttrForTops: function()
	{
		this.Tops[0].Node.setAttribute("cursor", "nw-resize");
		this.Tops[1].Node.setAttribute("cursor", "se-resize");
	},

	_arrangeTops: function()
	{
		var topRadius = SSVC.SVG.Shape.GetProperty("top");
		//todo: cap
		topRadius = 3;

		this.Tops[0].update(this.startX, this.startY, topRadius);

		this.Tops[1].update(this.endX, this.endY, topRadius);
	},

	_arrangeInnerElements: function()
	{
		this.inherited(arguments);
		this.InnerElement.update(this.startX, this.startY, this.endX, this.endY);
		this.Contour.update(this.startX, this.startY, this.endX, this.endY);
	},

	_move: function(x, y)
	{
		if (this.selectedPoint == null) return;

		var deltaX = x - this.selectedPoint.x;
		var deltaY = y - this.selectedPoint.y;

		this.startX += deltaX;
		this.startY += deltaY;
		this.endX += deltaX;
		this.endY += deltaY;

		this.inherited(arguments);
	},

	_resize: function(x, y, shiftKey)
	{
		var newcoordinates;
		var startX, startY;

		if (this.selectedTop == 1)
		{
			startX = this.endX;
			startY = this.endY;
		}
		else
		{
			startX = this.startX;
			startY = this.startY;
		}

		if (shiftKey)
		{
			var newPoint = this._getFixedCoordinates(startX, startY, x, y);
			x = newPoint.x;
			y = newPoint.y;
		}

		if (this.selectedTop == 1)
		{
			this.startX = x;
			this.startY = y;
		}
		else
		{
			this.endX = x;
			this.endY = y;
		}

		newcoordinates = this._getRectSpace(startX, startY, x, y);

		this.update(newcoordinates.x, newcoordinates.y, newcoordinates.ox, newcoordinates.oy);

		if (this.IsVector)
		{
			this.selectedPoint = { x: x, y: y };
			this._move(x, y);
		}

	},

	_getFixedCoordinates: function(startX, startY, currX, currY)
	{
		var newPoint = { x: currX, y: currY };
		var newcoordinates = this._getRectSpace(startX, startY, newPoint.x, newPoint.y);
		_getMovementDirection();

		switch (newcoordinates.side)
		{
			case 1:
				newPoint.x = startX + newcoordinates.ox;
				newPoint.y = startY + newcoordinates.oy;
				break;
			case 2:
				newPoint.x = startX + newcoordinates.ox;
				newPoint.y = startY - newcoordinates.oy;
				break;
			case 3:
				newPoint.x = startX - newcoordinates.ox;
				newPoint.y = startY - newcoordinates.oy;
				break;
			case 4:
				newPoint.x = startX - newcoordinates.ox;
				newPoint.y = startY + newcoordinates.oy;
				break;
		}

		return newPoint;

		function _getMovementDirection()
		{
			var line = Math.sqrt(Math.pow(newcoordinates.ox, 2) + Math.pow(newcoordinates.oy, 2));
			var arctgB = Math.atan(newcoordinates.ox / newcoordinates.oy);
			var currentAngle = arctgB * (180 / Math.PI);

			if (currentAngle >= 30 && currentAngle <= 70)
			{
				newcoordinates.oy = line * Math.sin(Math.PI / 4);
				newcoordinates.ox = line * Math.cos(Math.PI / 4);
			}
			else if (currentAngle < 40)
			{
				newcoordinates.oy = line;
				newcoordinates.ox = 0;
			}
			else
			{
				newcoordinates.oy = 0;
				newcoordinates.ox = line;
			}

		}
	},

	GetOuterXml: function()
	{
		var xml = this.InnerElement.getXml();

		xml.documentElement.setAttribute(this.axis.x, this.X);
		xml.documentElement.setAttribute(this.axis.ox, this.OX);
		xml.documentElement.setAttribute(this.axis.y, this.Y);
		xml.documentElement.setAttribute(this.axis.oy, this.OY);
		xml.documentElement.setAttribute("isvector", this.IsVector === true ? "true" : "false");

		if (this.IsVector == true)
			xml.removeAttribute("marker-end");
		return xml;
	},

	restoreCoo: function(xmlNode) // override for any shape
	{
		this.startX = +xmlNode.getAttribute(this.InnerElement.axis.x);
		this.startY = +xmlNode.getAttribute(this.InnerElement.axis.y);
		this.endX = +xmlNode.getAttribute(this.InnerElement.axis.ox);
		this.endY = +xmlNode.getAttribute(this.InnerElement.axis.oy);

		this.X = this.startX;
		this.Y = this.startY;
		this.OX = this.startX + this.endX;
		this.OY = this.startY + this.endY;

		this.IsVector = (xmlNode.getAttribute("isvector") == "true");
		if (this.IsVector)
			this.Vector();
	},

	Vector: function()
	{
		this.IsVector = true;
		this.InnerElement.setAttribute("marker-end", "url(#Triangle)");
	},

	ScaleX: function(factor)
	{
		if (factor <= 0)
			return;

		this.startX *= factor;
		this.endX *= factor;
		this.X *= factor;
		this.OX *= factor;
		this.Refresh();
	},

	ScaleY: function(factor)
	{
		if (factor <= 0)
			return;

		this.startY *= factor;
		this.endY *= factor;
		this.Y *= factor;
		this.OY *= factor;
		this.Refresh();
	},

	Draw: function(startX, startY, endX, endY, shiftKey)
	{
		this.startX = startX;
		this.startY = startY;

		if (this.startX == 0)
			this.startX = this.endX;
		if (this.startY == 0)
			this.startY = this.endY;

		if (shiftKey)
		{
			var newPoint = this._getFixedCoordinates(startX, startY, endX, endY);
			this.endX = newPoint.x;
			this.endY = newPoint.y;
			return;
		}

		this.endX = endX;
		this.endY = endY;
		this.inherited(arguments);
	}
});
SSVC.Wrap.Dojo.SetObject("SSVC.SVG.ActiveLineElement", ActiveLineElement);
